package com.ks.app.navigate.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

public class ActionResponseUtils {
	public static void writer(Object ob, PrintWriter out) throws IOException {
		out.print(ob);
	}

	public static void wirteAndEnd(Object ob, String charset, HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=" + charset);
		PrintWriter out = response.getWriter();
		writer(ob, out);
		out.flush();
		out.close();
	}

	public static void wirteAndEnd(Object ob, HttpServletResponse response) throws IOException {
		if(ob!=null){
		    wirteAndEnd(ob, "UTF-8", response);
		}
		else{
			 wirteAndEnd("{\"ret\":\"没有符合条件的数据!\"}", "UTF-8", response);
		}
	}
	
	/**
	 * 直接下载
	 * @param ob
	 * @param response
	 * @throws IOException
	 */
	public static void writeAndDownload(String ob, HttpServletResponse response)throws IOException{
		
		if(ob!=null){
			
			response.setContentType("application/x-msdownload");
			response.setContentType("application/wnd.wap.wmlc");
			response.setHeader("Content-disposition","attachment; filename=\"getweather.xml\"");    
			response.setContentLength(ob.getBytes("utf-8").length);
	
			PrintWriter out = response.getWriter();
			writer(ob, out);
			out.flush();
			out.close();
		}
		else{
			 wirteAndEnd("{\"ret\":\"没有符合条件的数据!\"}", "UTF-8", response);
		}
	}

	public static void writer(Object ob, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		writer(ob, out);
	}
}
